package com.qiku.service.impl;

import com.alibaba.fastjson.JSON;
import com.qiku.dao.CollectDao;
import com.qiku.dao.ResourceDao;
import com.qiku.dao.impl.CollectDaoImpl;
import com.qiku.dao.impl.ResourceDaoImpl;
import com.qiku.pojo.Resource;
import com.qiku.service.ResourceService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResourceServiceImpl implements ResourceService {
    private ResourceDao resourceDao = new ResourceDaoImpl();
    private CollectDao collectDao = new CollectDaoImpl();

    //查询资源信息列表
    @Override
    public String findResourceList(int num) {
        List<Map<String, String>> resourceList = resourceDao.findResourceList(num);
        String result = JSON.toJSONString(resourceList);
        return result;
    }

    /**
     * 使用resourceDao向表中插入数据
     *
     * @param res Resource对象
     * @return 0表示插入失败，1为插入成功
     */
    @Override
    public int updateResource(Resource res) {
        int i = resourceDao.updateResource(res);
        return i;
    }

    //根据资源名或上传用户名查询资源信息
    @Override
    public String findByNameOrUserName(String resourceName, String userName, String type) {
        List<Map<String, String>> mapList = resourceDao.findByNameOrUserName(resourceName, userName,type);
        return JSON.toJSONString(mapList);
    }

    //逻辑删除资源
    public String deleteResource(String resourceId){
        Integer integer = resourceDao.deleteResource(resourceId);
        Map<String,String> map = new HashMap<>();
        if (integer != null){
            if (integer == 1){
                map.put("deleteMsg","已成功切换为隐藏");
            }else {
                map.put("deleteMsg","切换为隐藏失败！");
            }
        }else {
            map.put("deleteMsg","后台发生了一个错误~");
        }
        String result = JSON.toJSONString(map);
        return result;
    }

    //逻辑恢复资源
    public String recoverResource(String resourceId){
        Integer integer = resourceDao.recoverResource(resourceId);
        Map<String,String> map = new HashMap<>();
        if (integer != null){
            if (integer == 1){
                map.put("deleteMsg","已成功切换为公开");
            }else {
                map.put("deleteMsg","切换为公开失败！");
            }
        }else {
            map.put("deleteMsg","后台发生了一个错误~");
        }
        String result = JSON.toJSONString(map);
        return result;
    }

    //根据资源id查询资源信息
    @Override
    public Resource findResourceById(String id) {
        return resourceDao.findResourceById(id);
    }

    @Override
    public String findResourceByType(String type) {
        List<Resource> resourceListByType = resourceDao.findResourceListByType(type);
        return JSON.toJSONString(resourceListByType);
    }
}
